<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ taglib uri="http://java.sun.com/jstl/core_rt" prefix="c"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
  <head>
    <title>event-rule-add</title>
    <%@ include file="../include/common.jsp"%>
    <%@ include file="../include/jbox.jsp"%>
    <%@ include file="../include/form.jsp"%>
    <%@ include file="../include/crontab.jsp"%>
    <%@ include file="../include/timeUnit.jsp"%>
    <style type="text/css">
		.tdValue{
			border: 1px solid #D6D3D6;
			line-height: 26px;
			height: 32px;
			padding-left: 5px;
			overflow: hidden;
		}
	</style>
    <script type="text/javascript">
	    var stu;
	    var expr = '';
	    function back(){
			window.history.back();
		}
		$(function(){
			$("#msg1").val('${msg}');
			$("#msg").val('${msg1}');
			stu = new SimpleTimeUnit("vailTime1","ddHHmm");
			stu.show().isRequiredSymbol();
	        ctd.setTU(stu);
			$("#eventTypeId").change(function(){
				//ajax获得此事件类别的 msg 
				 $.ajax({
						type: "post", 
						url: "${pageContext.request.contextPath}/event/event-rule!haveMsgByEventTypeId.action?resId="+$("#resId").val()+"&eventTypeId="+$("#eventTypeId").val(), //请求的action
						data: "",
						dataType: "json",
						success: function(data){ //结果
							$("#msg1").val(data.msg);
							$("#msg1").blur();
							$("#msg").val(data.msg1);
							if(data.recoverable == 0){
		       					$("#durationFitTr").css({"display":""});
			  					$("#keyWordTr").css({"display":""});
		   					} else{
		       					$("#durationFitTr").css({"display":"none"});
			   					$("#keyWordTr").css({"display":"none"});
		    			    } 
							$("#keyTr").css({"display":"none"});
							$("#objectExpr").val(data.objectExpr);
							expr = '';
						}
				});
				
				//赋值
				$("#expr1").val('');
				$("#expr").val('');
				$("#exprIsDefault").val('');
				$("#msgIsDefault").val('1');
				$("#exprArray").val('');
				$("#defineEventType").val('');
			});
			$.formValidator.initConfig({formid:"myform"
		          ,onerror:function(msg){ }
		          ,onsuccess:function(){
		        	  	add();
		          		return false;
		          }
		    });
			$("#eventTypeId").formValidator({onshow:"不能为空",onfocus:"不能为空",oncorrect:"输入正确"})
			 .functionValidator({
					    fun:function(val,elem){
					    	    if(val == null){
					    	        return "不能为空";
					    	    }
					    	    return true;
						}
			});
			$("#msg1").formValidator({onshow:"不能为空",onfocus:"不能为空",oncorrect:"输入正确"}).inputValidator({min:1,onerror:"不能为空"}).inputValidator({min:1,max:256,onerror:"长度非法"});
			$("#expr1").formValidator({onshow:"不能为空",onfocus:"不能为空",oncorrect:"输入正确"}).inputValidator({min:1,onerror:"不能为空"}).inputValidator({min:1,max:256,onerror:"长度非法"});
			$("#duration").formValidator({onshow:"请输入数字 长度(1-6)",onfocus:"请输入数字 长度(1-6)",oncorrect:"输入正确"}).inputValidator({min:1,onerror:"不能为空"}).inputValidator({min:1,max:5,onerror:"长度范围（1-6）"})
			  .functionValidator({
					    fun:function(val,elem){
					    	 var reg = /^[0-9]+$/;
					    	    if(!reg.test(val)){
					    	        return "格式不正确";
					    	    }
					    	    return true;
						}
			});
			$("#remarks").formValidator({onshow:"请输入字符串 长度(1-1024)",onfocus:"请输入字符串 长度(1-1024)",oncorrect:"输入正确"})
			.functionValidator({
			    fun:function(val,elem){
			    	    if(val.replace(/[^\x00-\xff]/g, "xx").length > 1024){
			    	        return "长度超出最大范围";
			    	    }
			    	    return true;
				}
	  		});
			$('#expr1').live('blur',function(){
				if($("#exprIsDefault").val() == '1'){
					expr = $("#expr").val();
				}
				if($("#exprIsDefault").val() == '0'){
					expr = $("#expr1").val();
				}
				if(expr != '' && (expr.indexOf("TP") >= 0 || expr.indexOf("tprop") >= 0)){
					$("#keyTr").css({"display":""});
				}else if(expr != ''){
					$("#keyTr").css({"display":"none"});
					$("#objectExpr").val('');
				}
			});
			$('#expr1').blur();
		})
		function vailTimeCheckboxClick(){
			var str=document.getElementById("vailTimeCheckbox");
			if(str.checked == true){
				$("#vailTimeTr").css({"display":""});
				$("#vailTimeTr1").css({"display":""});
			}else{
				$("#vailTimeTr").css({"display":"none"});
				$("#vailTimeTr1").css({"display":"none"});
			}
		}
		function add(){
			var vailTime = '';
			var str=document.getElementById("vailTimeCheckbox");
			if(str.checked == false){
				vailTime = '0';//全天
			}else{
		        var arrays = (ctd.results()).split(" ");
		        if ("?" == arrays[5]) {
		            if ("*" == arrays[3]) {
		            } else {
		                // 月暂停
		            	if(stu.results() > 3600*24*31){
		            		getTopWin(window).getMessageBox().alert("有效时间选择按月后，持续时间不能超过一个月","提示");
		            		return;
			            }
		            }
		        } else {
		            // 说明是按星期
		            if(stu.results() > 3600*24*7){
		            	getTopWin(window).getMessageBox().alert("有效时间选择按周后，持续时间不能超过一周","提示");
		            	return;
		            }
		        }
				vailTime = ctd.results()+'@'+stu.results();
			}
			if(expr != '' && (expr.indexOf("TP") >= 0 || expr.indexOf("tprop") >= 0)){
				if($("#objectExpr").val() == ''){
					getTopWin(window).getMessageBox().alert("事件表达式key值不能为空","提示");
	            	return;
				};
			}
			  //提交
			 $.ajax({
				   type: "post", 
				   url: "${pageContext.request.contextPath}/event/event-rule!save.action?vailTime="+vailTime+"&update=false"+"&confirmDuration="+tunit.results()+"&filterDuration="+funit.results(),
				   data: $("#myform").serialize(), //传的参数
				   dataType: "json",
				   success: function(data){ //结果
					   if(data == true){
						   location.assign("event-rule!common.action?id=${resId}&pId=${pId}&viewId=${viewId}");
					   }
					   if(data == false){
						   getTopWin(window).getMessageBox().alert("同一资源下同一事件类别和同一级别的事件规则只能有一条","提示");
					   }
				   }
			});
		}
		
		function addExpr(){
			var expr1 = $("#expr").val();
			if($("#exprIsDefault").val() == '0'){
				expr1 = $("#expr1").val();
			}
			var eventTypeId= $("#eventTypeId").val();
			var url ="iframe:event/event-rule!addExpr.action?resId="+${resId}+"&eventTypeId="+eventTypeId+"&expr1="+encodeURI(encodeURI(expr1))+"&exprIsDefault="+$("#exprIsDefault").val()+"&exprArray="+$("#exprArray").val()+"&defineEventType="+$("#defineEventType").val();
			var opt = { title: "新增事件表达式",width:580,height:365,buttons: {}};
			getTopWin(window).openjBox(url,opt,'addExpr-event-rule',window);
		}
		function addExprValue(expr1,expr,exprIsDefault,array,defineEventType) {
			$("#expr").val(expr);
			$("#expr1").val(expr1);
			$("#exprIsDefault").val(exprIsDefault);
			$("#exprArray").val(array);
			$("#defineEventType").val(defineEventType);
			$("#expr1").blur();
		} 
		function addMsg(){
			var msg1 = $("#msg1").val();
			var eventTypeId= $("#eventTypeId").val();
			var url ="iframe:event/event-rule!addMsg.action?resId="+${resId}+"&eventTypeId="+eventTypeId+"&msg1="+encodeURI(encodeURI(msg1))+"&msgIsDefault="+$("#msgIsDefault").val()+"&defineEventType="+$("#defineEventType").val();
			var opt = { title: "新增事件消息",width:580,height:380,buttons: {}};
			getTopWin(window).openjBox(url,opt,'addMsg-event-rule',window);
		}
		function addMsgValue(msg1,msg,msgIsDefault,defineEventType) {
			$("#msg1").val(msg1);
			$("#msg").val(msg);
			$("#msgIsDefault").val(msgIsDefault);
			$("#defineEventType").val(defineEventType);
			$("#msg1").blur();
		} 
		function pageReset(){
			$("#myform").get()[0].reset(); 
		 	Crontab.isClearDiv = true;
			Crontab.Method.initDiv("vailTime"); 
			stu = new SimpleTimeUnit("vailTime1");
			stu.show();
			$("#vailTimeTr").css({"display":"none"});
			$("#vailTimeTr1").css({"display":"none"});
			$("#msg1").val('${msg}');
			$("#msg").val('${msg1}');
			if('${recoverable}' == 0){
		       	$("#durationFitTr").css({"display":""});
			  	$("#keyWordTr").css({"display":""});
		   	} else{
		       	$("#durationFitTr").css({"display":"none"});
			   	$("#keyWordTr").css({"display":"none"});
		    } 
		}
    </script>
  </head>
  
  <body>
  		<h2 class="title-h2"><span>新增事件规则</span></h2>
  		<div id="menuContent" style="display:none; position: absolute;z-index:100;">
			<ul id="treeDemo" class="ztree" style="margin-top:0; width:200px; height: 200px;"></ul>
		</div>
    	<form action="" id="myform" name="myform" method="post">
    		<table class="table_eidt" >
    			<tr>
    				<th class="td_left" width="150px">
    					<span class="span_alert">*</span><label>事件类别</label>
    				</th>
    				<td class="td_right">
	    				<select name="eventTypeId" id="eventTypeId" style="width: 200px">
				          	<c:forEach items="${eventTypeList}" var="e">
				          		<option value=${e.id}>${e.name}</option>
							</c:forEach>
			        	</select>
	    				<span id="eventTypeIdTip" style="width:200px"></span>
    				</td>
    			</tr>
    			<tr>
    				<th class="td_left">
    					<label>事件级别</label>
    				</th>
    				<td class="td_right">
    					<input type="radio" name="level" id="level" value="2" ><label style="color: #C8FF00">轻微</label>
						<input type="radio" name="level" id="level" value="3" ><label style="color: #FFD200">警告</label>
						<input type="radio" name="level" id="level" value="4" checked="checked"><label style="color: #FF9200">重要</label>
						<input type="radio" name="level" id="level" value="5" ><label style="color: #FF1042">严重</label>
    				</td>
    			</tr>
    			<tr height="80px">
    				<th class="td_left">
    					<span class="span_alert">*</span><label>事件表达式</label>
    				</th>
    				<td class="td_right">
    					<textarea style="width: 600px;height: 50px;" id="expr1" name="expr1" onclick="addExpr();"></textarea>
    					<input type="hidden" id="expr" name="expr">
    					<span id="expr1Tip" style="width:200px"></span>
    				</td>
    			</tr>
    			<tr style="display: none;" id="keyTr">
    				<th class="td_left">
    					<span class="span_alert">*</span><label>表格数据KEY列</label>
    				</th>
    				<td class="td_right">
    					<input type="text" id="objectExpr" name="objectExpr" style="width: 500px;" value='${objectExpr}'>
    					<span id="objectExprTip" style="width:200px"></span>
    				</td>
    			</tr>
    			<tr>
    				<th class="td_left">
    					<label>事件阀值持续时间(秒)</label>
    				</th>
    				<td class="td_right">
    					<input type="text" id="duration" name="duration" value="0" style="width: 60px;">
    					<span id="durationTip" style="width:200px"></span>
    				</td>
    			</tr>
    			<tr>
	    			<td class="td_left">自动确认间隔时间</td>
	    			<td class="tdValue">
	    			<div id="divDuration"></div>
	    				<script type="text/javascript">
	    				 	tunit = new SimpleTimeUnit("divDuration","ddHHmm");
	    				 	tunit.show();
				        </script>
	    			</td>
    			</tr>
    			<tr style="display: none;" id="durationFitTr" >
	    			<td class="td_left">事件过滤持续时间</td>
	    			<td class="tdValue">
	    			<div id="divDurationFit"></div>
	    				<script type="text/javascript">
	    				 	funit = new SimpleTimeUnit("divDurationFit","ddHHmm");
	    				 	funit.show();
				        </script>
	    			</td>
    			</tr>
    			<tr style="display: none;" id="keyWordTr" >
	    			<td class="td_left">过滤内容</td>
	    			<td class="td_right"><textarea id="keyWord" name="keyWord" style="width: 600px;height: 50px"></textarea>
	    			<span id="keyWordTip" style="width:150px"></span></td>
    			</tr>
    			<tr>
    				<th class="td_left">
    					<span style="width:66px; height:30px; display:block; float:left;"><input style="margin-top:6px;" type="checkbox" id="vailTimeCheckbox" value="vailTimeCheckbox" onclick="vailTimeCheckboxClick();"></span>
    					<span><label>指定有效时间段</label></span>
    				</th>
    				<td class="td_right">
    					<label>默认为全天</label>
    				</td>
    			</tr>
    			<tr style="display: none;" id="vailTimeTr">
	    				<th class="td_left">
	    					<label>有效时间</label>
	    				</th>
	    				<td class="tdValue">
							<div id="vailTime"></div>
							 <script type="text/javascript">
			                     var ctd = new Crontabs("vailTime");
			                 </script>
	    				</td>
	    		</tr>
	    		<tr style="display: none;" id="vailTimeTr1">
	    				<th class="td_left">
	    					<label>持续时间</label>
	    				</th>
	    				<td class="tdValue">
							<div id="vailTime1"></div>
	    				</td>
	    		</tr>
    			<tr height="80px">
    				<th class="td_left">
    					<span class="span_alert">*</span><label>事件消息</label>
    				</th>
    				<td class="td_right">
	    				<textarea style="width: 600px;height: 50px;" id="msg1" name="msg1" onclick="addMsg();"></textarea>
    					<input type="hidden" id="msg" name="msg">
    					<span id="msg1Tip" style="width:200px"></span>
    				</td>
    			</tr>
    			<tr>
    				<th class="td_left">
    					<label>规则注释</label>
    				</th>
    				<td class="td_right">
    					<textarea style="width: 600px;height: 50px;" id="remarks" name="remarks"></textarea>
    					<span id="remarksTip" style="width:200px"></span>
    				</td>
    			</tr>
    		</table>
    		<input type="hidden" id="status" name="status" value="1">
    		<input type="hidden" id="resId" name="resId" value=${resId}>
    		<input type="hidden" id="exprIsDefault" name="exprIsDefault">
    		<input type="hidden" id="msgIsDefault" name="msgIsDefault" value="1">
    		<input type="hidden" id="exprArray" name="exprArray">
    		<input type="hidden" id="defineEventType" name="defineEventType" value="0">
    		<div style="margin-left:150px; margin-top:5px;">
		    	<button type="submit" id="button" name="button" class="btn-4">保存</button>
		    	<button type="button" class="btn-4" onclick="pageReset();">重置</button>
		    	<button type="button" onclick="back();" class="btn-4">返回</button>
    		</div>
    	</form>
  </body>
  
</html>
